package com.hsxc.cdz.monitor.executor;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.bean.Pile;
import com.hsxc.cdz.base.dao.SystemDao;
import com.hsxc.cdz.base.util.CommonUtils;
import com.hsxc.cdz.base.util.Constants;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.mgmt.pile.PileManager;
import com.hsxc.cdz.mgmt.user.IcUserManager;
import com.hsxc.cdz.monitor.FSM.CachePile;
import com.hsxc.cdz.monitor.bean.Message;
import com.hsxc.cdz.monitor.bean.MessageNew;
import com.hsxc.cdz.monitor.bean.pile.v1_33.*;
import com.hsxc.cdz.monitor.bean.server.v1_33.*;
import com.hsxc.cdz.trans.bill.BillManager;
import com.hsxc.cdz.trans.bill.CacheTrade;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 作者 : 陈钊
 * @version 创建时间：2016年9月22日 上午10:12:46 类说明：
 */
@Component
public class PileExecutorNew {

    @Autowired
    private PileManager pileRegister;
    @Autowired
    private SystemDao systemDao;
    @Autowired
    BillManager billManager;
    @Autowired
    IcUserManager icUserManager;
    // Logger
    private static Logger logger = LoggerFactory.getLogger(PileExecutorNew.class);

    public void load(String pileAddress, String operatorNO, RegisterPileMsg registerContent, String lineNum,
                     int pileType, String protocol, ChannelHandlerContext ctx) {

        // 从报文中获取充电桩数据
        Pile pile = new Pile();
        pile.setPileNO(pileAddress);
        pile.setOperatorNO(operatorNO);
        pile.setState(Pile.STATE_IDLE);
        pile.setSoftVersion(registerContent.getVersionNum());
        pile.setDelete(false);
        pile.setAccount(registerContent.getAccount());
        pile.setPassword(registerContent.getPassword());
        pile.setFstLevType(registerContent.getFirstType());
        pile.setScdLevType(registerContent.getSecondType());
        pile.setSn(registerContent.getSN());
        pile.setLongitude(((float) registerContent.getLongitudes()) / 1000000);
        pile.setLatitude(((float) registerContent.getLatitudes()) / 1000000);
        pile.setPhyType(pileType);
        // 检测数据库中是否存在该桩
        Pile check = pileRegister.selPile(pile.getPileNO());
        MessageNew message = new MessageNew();
        message.setIsRegister(Constants.NO_REGISTER);
        message.setResponse(Constants.RESPONSE_NOMAL);
        // 检测充电桩上传数据与数据库数据是否匹配
        // 根据检测结果设定返回充电桩的报文
        logger.info("电桩登录后，电桩状态更新啦：" + pile.getState());
        if (check != null) {
            if (!check.getAccount().equals(pile.getAccount())) {
                message.setResponse(Constants.RESPONSE_ERROR_ACCOUNT);
            } else if (!check.getPassword().equals(pile.getPassword())) {
                message.setResponse(Constants.RESPONSE_ERROR_PASSWORD);
            } else if (!check.getOperatorNO().equals(operatorNO)) {
                message.setResponse(Constants.RESPONSE_NOMAL);
                logger.info(message.getOperator() + "oooooooo");
            } else {
                // 将桩的数据放入缓存并更新数据库
                pile.setOperatorNO(check.getOperatorNO());
                pile.setStationNO(check.getStationNO());
                boolean success = GlobalManager.pileManager.loadPile(pile, ctx, protocol,pileType);
                if (!success) {
                    message.setResponse(Constants.RESPONSE_ERROR_RENAME);
                } else {
                    message.setIsRegister(Constants.DID_REGISTER);
                }
            }
        }

        // 生成回复报文体
        RegisterSerMsg regResponse = new RegisterSerMsg();
        regResponse.setOperator(operatorNO);
        regResponse.setAddress(pileAddress);
        regResponse.setSecretKey(13456789);
        regResponse.setVesion(registerContent.getVersionNum());
        // 设置时间间隔
        regResponse.setHeartInterval(10);
        regResponse.setRetryInterval(5);
        regResponse.setRetryCount(5);
        String ip = (systemDao.selectByName("ipaddress"));
        ip = (ip == null) ? "192.198.1.46" : ip;
        regResponse.setIpAdress(ip);
        String port = systemDao.selectByName("port");
        port = (port == null) ? "5000" : port;
        regResponse.setPort(Integer.valueOf(port));
        // 生成回复报文
        message.setAddress(pileAddress);
        message.setContent(regResponse.getBytesFromMessage());
        message.setContentLength(RegisterSerMsg.length);
        message.setFrame(Constants.SERVER_RESPONSE_REGISTER);
        message.setIsOpen(Constants.START_UP);
        message.setLineNum(lineNum);
        message.setOperator(operatorNO);
        ctx.writeAndFlush(message);

        // TODO:是否需要时间间隔？
        // 登录后马上设置二维码，二维码为设备地址的后12个字节（前4个字节为城市区号）
        ServerExecutor.setQRC(pileAddress, pileAddress, protocol, pileType);
    }

    public void register_zhiliu(String pileAddress, String operatorNO, RegisterPileMsg registerContent, String lineNum,
                                String protocol, int pileType, ChannelHandlerContext ctx) {
        // 获取充电桩数据
        Pile pile = new Pile();
        pile.setPileNO(pileAddress);
        pile.setOperatorNO(operatorNO);
        pile.setAccount(registerContent.getAccount());
        pile.setPassword(registerContent.getPassword());
        // 生成桩的对象
        pile.setState(Pile.STATE_IDLE);
        pile.setSoftVersion(registerContent.getVersionNum());
        pile.setDelete(false);
        pile.setRegTime(TimeFormatter.toTimeYear());
        pile.setFstLevType(registerContent.getFirstType());
        pile.setScdLevType(registerContent.getSecondType());
        pile.setSn(registerContent.getSN());
        pile.setLongitude(((float) registerContent.getLongitudes()) / 1000000);
        pile.setLatitude(((float) registerContent.getLatitudes()) / 1000000);
        pile.setPhyType(pileType);// 直流桩、交流桩
        // 无法判断属于私人还是电站

        Pile check = pileRegister.selPile(pile.getPileNO());
       logger.info(pileAddress);

        MessageNew message = new MessageNew();
        message.setIsRegister(Constants.NO_REGISTER);
        message.setResponse(Constants.RESPONSE_NOMAL);
        message.setProtocolVersion(protocol);
        message.setPileType(pileType);
        // 检测充电桩上传数据与数据库数据是否匹配
        // 根据检测结果设定返回充电桩的报文
        if (check != null) {
            if (!check.getAccount().equals(pile.getAccount())) {
                message.setResponse(Constants.RESPONSE_ERROR_ACCOUNT);
            } else if (!check.getPassword().equals(pile.getPassword())) {
                message.setResponse(Constants.RESPONSE_ERROR_PASSWORD);
            } else if (!check.getOperatorNO().equals(operatorNO)) {
                message.setResponse(Constants.RESPONSE_NOMAL);
            } else {
                boolean success = false;
                success = GlobalManager.pileManager.registerPile(pile, ctx, protocol);
                if (!success) {
                    message.setResponse(Constants.RESPONSE_ERROR_RENAME);
                } else {
                    message.setIsRegister(Constants.DID_REGISTER);
                }
            }
        }
        // 生成回复报文
        message.setAddress(pileAddress);
        // message.setContent(regResponse.getBytesFromMessage(registerContent.getProtocolVersion()));
        message.setFrame(Constants.SERVER_RESPONSE_REGISTER);
        message.setIsOpen(Constants.START_UP);
        message.setLineNum(lineNum);
        message.setOperator(operatorNO);
        // 生成服务端回复报文体
        RegisterSerMsg registerSerMsg = new RegisterSerMsg();
        registerSerMsg.setAddress(pileAddress);
        registerSerMsg.setOperator(operatorNO);
        registerSerMsg.setSecretKey(13456789);
        registerSerMsg.setVesion(registerContent.getVersionNum());
        String ip = (systemDao.selectByName("ipaddress"));
        ip = (ip == null) ? "192.168.1.122" : ip;
        registerSerMsg.setIpAdress(ip);
        String port = systemDao.selectByName("port");
        port = (port == null) ? "5000" : port;
        registerSerMsg.setPort(Integer.valueOf(port));
        registerSerMsg.setHeartInterval(10);// 设置间隔
        registerSerMsg.setRetryInterval(5);// 重试间隔
        registerSerMsg.setRetryCount(5);// 重试次数
        // 设置报文长度及报文内容
        message.setContentLength(RegisterSerMsg.length);
        message.setContent(registerSerMsg.getBytesFromMessage());

        // 写出回复报文
        ctx.writeAndFlush(message);

        // 注册后马上设置二维码，二维码为设备地址的后12个字节（前4个字节为城市区号）
        ServerExecutor.setQRC(pileAddress, pileAddress, protocol, pileType);

    }

    /**
     * 充电桩退出登录
     *
     * @param pileAddress ：充电桩上传给服务器的报文的实体类
     * @return message：服务器下发给充电桩的实体类
     */
    public boolean unload(String pileAddress, String operatorNO, String lineNum, String protocol, int pileType) {
        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            return false;
        }
        int i = cachePile.logout();
        MessageNew message = new MessageNew();
        if (i == 0) {
            message.setResponse(Constants.RESPONSE_NOMAL);
        } else {
            message.setResponse(Constants.RESPONSE_ERROR_COMMAND);
        }
        message.setIsOpen(Constants.START_UP);
        message.setIsRegister(Constants.DID_REGISTER);
        message.setLineNum(lineNum);
        message.setAddress(cachePile.getAddress());
        message.setOperator(cachePile.getOperator());
        message.setFrame(Constants.SERVER_RESPONSE_UNLOAD);
        message.setContent(null);
        message.setContentLength(0);
        message.setPileType(pileType);
        message.setProtocolVersion(protocol);

        Channel channel = cachePile.getChannel();
        if (channel == null) {
            return false;
        }
        channel.writeAndFlush(message);
        return true;
    }

    public boolean heart(String pileAddress, HeartPileMsg heart, String lineNum) {
        /*
		 * System.out.println("心跳设备地址" + pileAddress); CachePile cachePile =
		 * GlobalManager.pileManager.getPile(pileAddress); if (cachePile ==
		 * null) { return false; }
		 * 
		 * // 更新交易、账单 GlobalManager.tradeManager.updateTradeByHeart(pileAddress,
		 * heart.getElectricity(), heart.getSumMoney());
		 * GlobalManager.billManager.updateBillByHeart(pileAddress,
		 * heart.getElectricity(), heart.getSumMoney());
		 */
		/*
		 * TODO: if (heart.getChargeState() == HeartPileMsg.CHARGE_STATE_WORK &&
		 * cachePile.getStatus() != PileMemState.Status.PILE_STATUS_LOCKED) {
		 * cachePile.changeStateCharge(); } if (heart.getChargeState() ==
		 * HeartPileMsg.CHARGE_STATE_IDLE && cachePile.getStatus() !=
		 * PileMemState.Status.PILE_STATUS_LOCKED) {
		 * cachePile.changeStateIdle(); } if (heart.getReserveState() ==
		 * HeartPileMsg.RESERVE_STATE_WORK && cachePile.getStatus() !=
		 * PileMemState.Status.PILE_STATUS_LOCKED) { cachePile.setReserve(true);
		 * }
		 */
		/*
		 * if (!heart.getFaultCode().equals("00")) {
		 * 
		 * } cachePile.setFault(heart.getFaultCode()); Message message = new
		 * Message(); message.setAddress(pileAddress);
		 * message.setIsOpen(Constants.START_UP);
		 * message.setIsRegister(Constants.DID_REGISTER);
		 * message.setLineNum(lineNum);
		 * message.setOperator(cachePile.getOperator());
		 * message.setResponse(Constants.RESPONSE_NOMAL);
		 * message.setFrame(Constants.SERVER_RESPONSE_HEART);
		 * message.setContentLength(0); message.setContent(null); Channel
		 * channel = cachePile.getChannel(); if (channel != null) {
		 * channel.writeAndFlush(message); return true; } else { return false; }
		 */
        return true;
    }

    public boolean startCharge(ChargePileMsg startContent, String pileAddress, String lineNum, String protocol,
                               int pileType) {

        logger.info("startContent-->>" + startContent.getCardNum());
        logger.info("pileAddress-->>" + pileAddress);

        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            return false;
        }
        // Add to charging list
        // ActiveMonitor.addPileToList(cachePile);

        boolean b = cachePile.confirmStart(0, 0, 0, 0, 1.1, pileAddress, startContent.getCardNum());

        MessageNew message = new MessageNew();
        message.setIsRegister(Constants.DID_REGISTER);
        if (b) {
            message.setResponse(Constants.RESPONSE_NOMAL);
        } else {
            return true;
        }
        message.setIsOpen(Constants.START_UP);
        message.setFrame(Constants.SERVER_RESPONSE_START);
        message.setLineNum(lineNum);
        ChargeSerMsg response = new ChargeSerMsg();
        response.setChargePort(startContent.getChargePort());
        response.setControll(startContent.getControll());
        response.setStartTime(startContent.getOntimeStart());
        response.setLimitData(startContent.getLimitData());
        response.setCardNum(startContent.getCardNum());
        // 协议说明可以传空
        message.setContent(response.getBytesFromMessage());
        message.setContentLength(ChargeSerMsg.length);
        message.setAddress(pileAddress);
        message.setOperator(cachePile.getOperator());
        message.setPileType(pileType);
        message.setProtocolVersion(protocol);

        logger.info("回复充电开始");
        cachePile.getChannel().writeAndFlush(message);
        return true;
    }

    public boolean stopCharge(CharageCeasePileMsg stop, String address, String lineNum, String protocol, int pileType) {

        logger.info("电桩[编号]" + address + "累计充电时间为" + stop.getChargeTime() + "停止时荷电状态" + stop.getSOCEnd());
        // 获取缓存中桩的数据
        CachePile cachePile = GlobalManager.pileManager.getPile(address);
        if (cachePile == null) {
            return false;
        }
        // 确认充电停止
        boolean b = cachePile.confirmStop(stop.getPort(), 0, stop.getCardNum());
        // 生成回复桩停止充电的报文体
        ChargeCeaseSerMsg stopResp = new ChargeCeaseSerMsg();
        stopResp.setPort(stop.getPort());
        stopResp.setControll(stop.getControll());
        stopResp.setCardNum(stop.getCardNum());
        // 生成回复报文
        MessageNew message = new MessageNew();
        message.setIsRegister(Constants.DID_REGISTER);
        if (b) {
            message.setResponse(Constants.RESPONSE_NOMAL);
        } else {
            return true;
        }
        message.setIsOpen(Constants.START_UP);
        message.setLineNum(lineNum);
        message.setFrame(Constants.SERVER_RESPONSE_STOP);
        message.setContentLength(ChargeCeaseSerMsg.length);
        message.setContent(stopResp.getBytesFromMessage());
        message.setOperator(cachePile.getOperator());
        message.setAddress(address);
        message.setPileType(pileType);
        message.setProtocolVersion(protocol);

       logger.info("回复充电结束");
        cachePile.getChannel().writeAndFlush(message);
        return true;
    }

    /**
     * 充电桩回复预约 回复内容是修改预约的开始时间
     *
     * @param reservPileMsg
     * @param pileAddress
     * @param lineNum
     */
    public boolean respReserve(ReservPileMsg reservPileMsg, String pileAddress, String lineNum, String protocol,
                               int pileType) {
        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            return false;
        }
        // 调用电桩的方法确认预约成功
        // 交流电预约时，开始时间不需要传递
        return cachePile.confirmReserv(reservPileMsg.getPort(), reservPileMsg.getCardNo(),
                reservPileMsg.getResStartTime());

        // 协议说明收到电桩确认后不需要发送确认

		/*
		 * ReserveSerMsg reserveSerMsg = new ReserveSerMsg();
		 * reserveSerMsg.setCardNum(reservPileMsg.getCardNo());
		 * reserveSerMsg.setChargePort(reservPileMsg.getPort());
		 * 
		 * Message message = new Message();
		 * message.setIsOpen(Constants.START_UP); message.setLineNum(lineNum);
		 * message.setAddress(pileAddress);
		 * message.setOperator(cachePile.getOperator());
		 * message.setFrame(Constants.SERVER_RESPONSE_STOP_ORDER);
		 * message.setContentLength(ReserveCeaseSerMsg.length);
		 * message.setContent(reserveSerMsg.getBytesFromMessage());
		 * message.setResponse(Constants.RESPONSE_NOMAL);
		 * message.setIsRegister(Constants.DID_REGISTER); Channel channel =
		 * cachePile.getChannel(); if (channel == null) { return false; }
		 * channel.writeAndFlush(message);
		 */
    }

    /**
     * 充电桩停止预约主动上送
     *
     * @param stopOrder    ：停止预约充电桩的报文体
     * @param pileAddress： 充电桩设备地址
     * @return 服务器下发的报文体
     */
    public boolean stopReserve(ReserveCeasePileMsg stopOrder, String pileAddress, String lineNum, String protocol,
                               int pileType) {
        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            return false;
        }
        // 确认预约取消
        cachePile.confirmReservCancelled(stopOrder.getPort(), stopOrder.getCardNum());

        ReserveCeaseSerMsg stopResponse = new ReserveCeaseSerMsg();
        stopResponse.setPort(stopOrder.getPort());
        stopResponse.setCardNum(stopOrder.getCardNum());
        // 协议说明只需要回复0x58
        MessageNew message = new MessageNew();
        message.setIsOpen(Constants.START_UP);
        message.setLineNum(lineNum);
        message.setAddress(pileAddress);
        message.setOperator(cachePile.getOperator());
        message.setFrame(Constants.SERVER_RESPONSE_STOP_ORDER);
        message.setContentLength(ReserveCeaseSerMsg.length);
        message.setContent(stopResponse.getBytesFromMessage());
        message.setResponse(Constants.RESPONSE_NOMAL);
        message.setIsRegister(Constants.DID_REGISTER);
        message.setPileType(pileType);
        message.setProtocolVersion(protocol);

        Channel channel = cachePile.getChannel();
        if (channel == null) {
            return false;
        }
        channel.writeAndFlush(message);
        return true;
    }

    /**
     * @param stateChangePM :开始充电报文
     * @param address       :充电桩设备地址
     * @return 是否成功
     */
    public boolean changeState(StateChangePileMsg stateChangePM, String address) {

        int parameter = stateChangePM.getParameter();
        int value = stateChangePM.getValue();
        CachePile cachePile = GlobalManager.pileManager.getPile(address);
        if (cachePile == null) {
            return false;
        }
        if (parameter == StateChangePileMsg.CHARGE_STATE) {
            switch (value) {
                case StateChangePileMsg.STATE_TRUE:
                    cachePile.changeStateCharge();
                case StateChangePileMsg.STATE_FALSE:
                    cachePile.changeStateIdle();
            }
        } else if (parameter == StateChangePileMsg.FAULT_STATE) {
            switch (value) {
                case StateChangePileMsg.STATE_TRUE:
                    cachePile.fault();
                case StateChangePileMsg.STATE_FALSE:
                    cachePile.recovery();
            }
        } else if (parameter == StateChangePileMsg.PLUG_STATE) {
            switch (value) {
                case StateChangePileMsg.STATE_TRUE:
                    cachePile.setInserted(true);
                case StateChangePileMsg.STATE_FALSE:
                    cachePile.setInserted(false);
            }
        }
        return true;
    }

    /**
     * 充电桩响应重启
     *
     * @param pileAddress
     * @return
     */
    public void restart(String pileAddress) {
        if (pileAddress == null) {
            return;
        }
        CachePile pile = GlobalManager.pileManager.getPile(pileAddress);
        if (pile == null) {
            return;
        }
        pile.confirmReboot();
    }

    /**
     * 存入账单
     *
     * @param billContent：充电桩上送的账单报文体
     * @param pileAddress：充电桩设备地址
     * @return 执行标识符 0：成功，1：失败
     */
    public boolean storeBill(BillPileMsg billContent, String pileAddress, String lineNum) {
        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            return false;
        }
        // 此处账单没有账单编号
        Bill bill = new Bill();
        bill.setUserNO(billContent.getCardNum());
        bill.setPileNO(pileAddress);
        bill.setGenerateTime(TimeFormatter.toTimeYear());
        bill.setStartTime(billContent.getStartTime());
        bill.setStopTime(billContent.getEndTime());
        bill.setChargeFee(((double) billContent.getSumMoney()) / 100);
        bill.setServiceFee(((double) billContent.getserviceMon()) / 100);

        bill.setFeeSum(bill.getChargeFee() + bill.getServiceFee());
        bill.setState(billContent.getIsPay());
        bill.setPayTime(null);
        bill.setDegreeBefore(((double) billContent.getStartRecord()) / 100);
        bill.setDegreeAfter(((double) billContent.getEndRecord()) / 100);
        bill.setChargeDegree(((double) billContent.getElectricity()) / 100);
        bill.setBalanceIcBefore(((double) billContent.getStartCardMon()) / 100);
        bill.setBalanceIcAfter(((double) billContent.getEndCardMon()) / 100);

        CacheTrade cacheTrade = GlobalManager.tradeManager.getTrade(pileAddress, billContent.getCardNum());
        if (cacheTrade == null) {
            // 为刷卡账单直接创建账单
            bill.setBillNO(CommonUtils.getTID());
            bill.setPayTime(TimeFormatter.toTimeYear());
            bill.setPayWay(Bill.CARD);
            GlobalManager.billManager.createBill(bill);
        } else {
            logger.info("------storebill---------确认停止时交易存在------------");
            // 当交易存在时，不是刷卡账单
            cachePile.stopTrade(pileAddress, billContent.getCardNum(), billContent.getElectricity(),
                    billContent.getSumMoney());
            bill.setBillNO(cacheTrade.getTid());
            bill.setState(Bill.NO_PAY);
            cachePile.stopBill(bill);
        }
        Message message = new Message();
        message.setIsOpen(Constants.START_UP);
        message.setIsRegister(Constants.DID_REGISTER);
        message.setLineNum(lineNum);
        message.setContent(null);
        message.setContentLength(0);
        message.setFrame(Constants.SERVER_RESPONSE_BILL);
        message.setResponse(Constants.RESPONSE_NOMAL);
        message.setAddress(cachePile.getAddress());
        message.setOperator(cachePile.getOperator());
        Channel channel = cachePile.getChannel();
        if (channel == null) {
            return false;
        }
        channel.writeAndFlush(message);
        return true;
    }

    /*
     * 处理刷卡请求
     */
    public boolean card(CardPileMsg card, String pileAddress, String lineNum, String protocol, int pileType) {

        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            return false;
        }
        icUserManager.select(card.getCardNum());
        CardSerMsg response = new CardSerMsg();
        response.setChargePort(card.getChargePort());
        response.setCardNum(card.getCardNum());
        response.setIsValid(0);

        MessageNew message = new MessageNew();
        message.setIsOpen(Constants.START_UP);
        message.setIsRegister(Constants.DID_REGISTER);
        message.setLineNum(lineNum);
        message.setAddress(cachePile.getAddress());
        message.setContentLength(CardSerMsg.length);
        message.setContent(response.getBytesFromMessage());
        message.setFrame(Constants.SERVER_RESPONSE_CARD);
        message.setOperator(cachePile.getOperator());
        message.setResponse(Constants.RESPONSE_NOMAL);
        message.setPileType(pileType);
        message.setProtocolVersion(protocol);

        Channel channel = cachePile.getChannel();
        if (channel == null) {
            return false;
        }
        channel.writeAndFlush(message);
        return true;
    }

    public boolean uploadChargeData(UploadChargeDataPileMsg uploadChargeDataPileMsg, String pileAddress, String lineNum,
                                    String protocol, int pileType) {
        CachePile cachePile = GlobalManager.pileManager.getPile(pileAddress);
        if (cachePile == null) {
            logger.info("在更新缓存桩中充电数据时，桩为空了");
            return false;
        }
        ChargeData chargeData = new ChargeData();
        chargeData.setChargePort(uploadChargeDataPileMsg.getChargePort());
        chargeData.setChargeTime(uploadChargeDataPileMsg.getChargeTime());
        chargeData.setChargeElectricity(uploadChargeDataPileMsg.getChargeElectricity());
        chargeData.setChargeVoltage(uploadChargeDataPileMsg.getChargeVoltage());
        chargeData.setCurrentRecord(uploadChargeDataPileMsg.getCurrentRecord());
        chargeData.setLeftElectricity(uploadChargeDataPileMsg.getLeftElectricity());
        chargeData.setMaxVoltageAndTeamNo(uploadChargeDataPileMsg.getMaxVoltageAndTeamNo());
        chargeData.setOutputPower(uploadChargeDataPileMsg.getOutputPower());
        chargeData.setPortState(uploadChargeDataPileMsg.getPortState());
        chargeData.setReadTime(uploadChargeDataPileMsg.getReadTime());
        chargeData.setSOC(uploadChargeDataPileMsg.getSOC());
        chargeData.setStartRecord(uploadChargeDataPileMsg.getStartRecord());
        cachePile.setChargeData(chargeData);
        int result = GlobalManager.pileManager.newPile(cachePile);
        if (result == 0) {
            return true;
        } else {
            return false;
        }

    }

    public boolean queryRate(String pileAddress, QueryRatePileMsg content) {
        return true;
    }
}
